package com.carlinfo.back.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.userscenter.users.pojo.AAdmins;

/**
 * 过滤器====保安
 * ~放行
 * ~拦截
 * @author WangshMac
 */
public class AuthFilter implements Filter
{
	private Logger logger = LogManager.getLogger() ; 
	
	@Override
	public void destroy()
	{
		this.logger.info("==销毁==");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException
	{
		this.logger.info("==保安工作==");
		/* 登陆的时候往session放了一个adminsSess;票 */
		HttpServletRequest request = (HttpServletRequest) req ; 
		HttpServletResponse response = (HttpServletResponse) res ;
		/* 获取Session */
		HttpSession session = request.getSession() ;
		/* 取票:admins */
		AAdmins admins = (AAdmins) session.getAttribute("adminsSess");
		if(admins != null)
		{
			/* 有票,走的是正常的登陆 */
			/* 放行 */
			chain.doFilter(req, res);
		}else
		{
			String info = "你想跳墙,不可能,买票去" ; 
			/* 客户端跳转,url发生了变化,request不行 */
			session.setAttribute("info", info);
			/* 木有票,跳转到登陆页面,买票去
			 * 登陆去
			 * 客户端跳转
			 *  */
			String path = request.getContextPath() + "/NoLoginBackServlet?method=login" ; 
			response.sendRedirect(path);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		this.logger.info("==初始化==");
	}

}
